package com.data.hijack;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.data.hijack.constant.Constants;
import com.data.hijack.context.HolderContext;
import com.data.hijack.service.Service;
import com.data.hijack.util.AppUtils;
import com.data.hijack.util.StringUtils;
import com.data.hijack.util.TextUtils;

/**
 * 
 * @author husq
 */
public class ServiceBootStrap {
    private static final Logger logger = Logger.getLogger(ServiceBootStrap.class);
    private static final String IS_BOOT = "isBoot";
    private static final String BOOT_1 = "1";

    public static void boot() {
        Map<String, Object> configContext = (Map<String, Object>)HolderContext.get(Constants.CONFIG_CONTEXT);
        List<String> bizNameList = (List<String>)configContext.get(Constants.BIZ_NAMES);
        int size = bizNameList.size();
        for (int i = 0; i < size; i++) {
            String bizName = bizNameList.get(i);
            Map<String, String> bizConfig = (Map<String, String>)configContext.get(bizName);
            String stateValue = AppUtils.getValue(bizName, Constants.STATUS, bizConfig);
            if (Constants.STATUS_0.equals(stateValue.trim())) {
                continue;
            }

            String hostValue = AppUtils.getValue(bizName, Constants.LOCAL_HOST, bizConfig);
            String portValue = AppUtils.getValue(bizName, Constants.LOCAL_PORT, bizConfig);
            String isBootValue = AppUtils.getValue(bizName, IS_BOOT, bizConfig);
            if (BOOT_1.equals(isBootValue)) {
                continue;
            }

            String comment = AppUtils.getValue(bizName, Constants.COMMENT, bizConfig);
            String reusableBizName = AppUtils.getValue(bizName, Constants.LOCAL_REUSABLE_FROM, bizConfig);
            if (StringUtils.isNotBlank(reusableBizName)) {
                Map<String, String> depBizConfig = (Map<String, String>)configContext.get(reusableBizName.trim());
                isBootValue = AppUtils.getValue(reusableBizName, IS_BOOT, depBizConfig);
                hostValue = AppUtils.getValue(reusableBizName, Constants.LOCAL_HOST, depBizConfig);
                portValue = AppUtils.getValue(reusableBizName, Constants.LOCAL_PORT, depBizConfig);

                String depComment = AppUtils.getValue(reusableBizName, Constants.COMMENT, depBizConfig);
                stateValue = AppUtils.getValue(reusableBizName, Constants.STATUS, depBizConfig);
                if (Constants.STATUS_0.equals(stateValue.trim())) {
                    depComment = comment;
                    reusableBizName = bizName;
                } else {
                    start(hostValue, portValue, comment, bizName, false);
                }

                if (StringUtils.isBlank(isBootValue) || !BOOT_1.equals(isBootValue)) {
                    String isBoot = reusableBizName + Constants.DOT + IS_BOOT;
                    depBizConfig.put(isBoot, BOOT_1);
                    start(hostValue, portValue, depComment, reusableBizName, true);
                }

                continue;
            } else {
                start(hostValue, portValue, comment, bizName, true);
                String isBoot = bizName + Constants.DOT + IS_BOOT;
                bizConfig.put(isBoot, BOOT_1);
            }
        }
    }

    private static void start(String hostname, String port, String bizComment, String bizName, boolean start) {
        if (StringUtils.isNotBlank(hostname) && StringUtils.isNotBlank(port) && start) {
            Service s = new Service(hostname, Integer.parseInt(port), bizName);
            s.start();
        }

        logger.warn(TextUtils.format("%s[%s(%s)][%s, %s]", "The service has been started", bizComment, bizName,
            hostname, port));
    }
}
